//
//  LX_TabBar.swift
//  swiftWeiBo
//
//  Created by 轩 on 16/7/18.
//  Copyright © 2016年 KXuan. All rights reserved.
//

import UIKit

class LX_TabBar: UITabBar {

    ///一点击发布按钮的闭包
    var composeContent:(() ->())?
    
    lazy var composeBtn: UIButton = {
        let btn = UIButton(type: UIButtonType.Custom)
        
        
        
        //设置btn的图片
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setImage(UIImage(named:"tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        
        return btn
    }()
    
    
    override func layoutSubviews() {
           super.layoutSubviews()
        
        let itemCount = 5
        let newItemWidth = self.frame.width / CGFloat(itemCount)
        var index = 0
        
        for item in self.subviews
        {
            //判断item是否是uiTbarBarItem属性
            if item.isKindOfClass(NSClassFromString("UITabBarButton")!)
            {
                print("\(item)")
                //对UITabBarButton进行布局
                item.frame = CGRect.init(x: CGFloat(index)*newItemWidth, y: CGFloat(1), width: newItemWidth, height: CGFloat(self.frame.height))
                index++
                if index == 2
                {
                    index++
                }
            }
          
        }
        
        self.addSubview(self.composeBtn)
        //添加发布按钮
        composeBtn.frame = CGRect.init(x: newItemWidth*2, y: CGFloat(1), width: newItemWidth, height: CGFloat(self.frame.height))
        
        composeBtn.addTarget(self, action: "composeBtnDidClick", forControlEvents: UIControlEvents.TouchUpInside)
        
    }

    //MARK: - action
    ///发布按钮的点击事件
    func composeBtnDidClick() ->Void
    {
        //当前是在view上，所以要回到控制器，处理person出来的model控制器。
        
        //调用自已的闭包。
        composeContent!()
        
    }
}
